
Lesson 1
Introduction

     This program, CPUStructure, explains the inner workings of a hypothetical computer's Central Processing Unit (CPU). Though the CPU studied in this program is hypothetical, its basic principles of operation apply to most real-world CPUs.
     The program is broken up into 20 lessons that progress from the very basic components of a computer (digital logic gates) up to an example of the operation of a complete CPU.
     Because each lesson is dependent on material presented in previous lessons, it is highly recommended that the twenty lessons be studied in order.
     
Overview
     
     The CPU is the "brains" of the computer. Its function is to execute programs stored in main memory by fetching their instructions, examining them, and then executing them one after another. The CPU consists of three main parts:

Control Unit -
responsible for fetching instructions from main memory and determining their type.

Arithmetic Logic Unit -
performs mathematical and logical operations in order to carry out the instructions.

Scratchpad Registers -
a small number of registers that make up a small, high-speed memory used to store results and control information. One of these registers is the Program Counter (PC), which keeps track of the next instruction that is to be executed in main memory. Another scratchpad register is the Instruction Register (IR), which holds the instruction that is currently being executed.

     The CPU is driven by a set of internal, inalterable instructions. These instructions, called microinstructions, are held in a section of Read Only Memory (ROM) within the CPU.
     The microinstructions are in the form of a small computer program. They allow the CPU to carry out its functions, such as retrieving (fetching) code from main memory and performing mathematical and logical operations.
     The microinstructions are carried out one after another, in sequential order, unless code in main memory directs the CPU to use a microinstruction that is out of sequence. 
     The code in Main Memory is the code of the application (program) that is currently running. The CPU continually fetches pieces of this code. The code instructs the CPU as to which of its microinstructions it should use.
     The execution of a program (such as MacPaint, MacWrite, etc) causes the CPU to continuously cycle through its internal set of microinstructions. The cycle can be outlined as:

1. Fetch the next piece of code (instruction) from memory
2. Change the Program Counter so that it points to the following instruction
3. Determine the type of instruction just fetched
4. If the instruction uses data in memory, determine where the data is
5. Fetch the data, if any, into internal CPU scratchpad registers
6. Execute the instruction
7. Store the results in the proper place.
8. Go to step 1 (repeat the cycle)


     As you study the Lessons, keep in mind the difference between instructions and microinstructions:

INSTRUCTIONS -
are contained in the code of the program that the computer is currently executing (running). They are fetched from main memory by the CPU. They tell the CPU which of its microinstructions it should use.

MICROINSTRUCTIONS -
are permanently contained within the Control Unit of the CPU. They contain information that other parts of the CPU use in order to carry out the action specified by the INSTRUCTION from memory. 

		 The differences between instructions and microinstructions will become more apparent as you reach the final Lessons.


THE GRAPHIC WINDOW

		 The simulation in the Graphic Window demonstrates the above eight steps that the CPU continuously cycles through.


Lesson 2 
Digital Logic

     At the lowest level, computer hardware is made up of digital logic gates. They are the basic elements from which all digital computers are constructed.
     A digital logic gate uses only two logical values: "0" and "1". The gate interprets a voltage signal that is between 0 and 1 volt as a binary 0. It interprets a voltage signal that is between 2 and 5 volts as a binary 1. Voltages that do not fall within this range are not permitted.
     Voltages that are interpreted as binary 1 are referred to as HIGH or Vcc. Voltages that are interpreted as binary 0 are referred to as LOW or ground.
     Digital gates are small electronic devices that take in these voltage signals, interpret them as "1's" or "0's", and then output a voltage signal, which is also either a "1" or a "0".
     The six principle digital logic gates are pictured in the Graphics Window. The name and function of each is listed below:

OR gate - 
if any one or more of the gate's inputs has a value of "1", the output of the gate is a "1". If none of the inputs is a "1", the output is a "0". 

AND gate -
if and only if all of the inputs are "1's", the output of the gate is a "1". If any of the inputs is a "0", the output is a "0".

NOT gate (inverter) -
the output of the gate has a value opposite of the gate's one input value: if the input is a "0", the output is a "1". If the input is a "1", the output is a "0".

NOR gate -
is the opposite of an OR gate. If any one or more of the gates inputs has a value of "1", the output of the gate is a "0". If none of the inputs is a "1", the output is a "1".

XOR gate -
exclusive OR gate. If none or both of the gate's inputs has a value of "1", the output of the gate is a "0". If one of the inputs is a "1", the output is a "1".

NAND gate -
is the opposite of an AND gate. If and only if all of the inputs are "1's", the output of the gate is a "0". If any of the inputs is a "0", the output is a "1".

     By combining these six types of logic gates into electrical circuits, digital circuits are constructed.

     All logic gates have just one output. A NOT gate, also called an inverter, may have only one input. The other five gate types, pictured in the Graphics Window with two inputs, may have two or more inputs. The same principles apply to a logic gate regardless of the number of inputs it has.


THE GRAPHIC WINDOW

		 The Graphic Window shows each of the five basic gates mentioned above. The Edit Boxes serve as the inputs to each gate. Enter a "1" or a "0" in each of the Edit Boxes, then run the simulation to see the gate outputs.


Lesson 3 
Multiplexer

		 A multiplexer (also called a MUX) is a circuit with two or more data inputs, one data output, and control (select) lines. The number of data inputs that the circuit may have is dependent on the number of control lines. If a multiplexer has n control lines, it can then handle 2^n data inputs. All multiplexers have just one output.
		 The number of data inputs gives the multiplexer its specific name: a four input, one output multiplexer is called a 4-to-1 MUX, an eight input multiplexer is called an 8-to-1 MUX, etc. 
		 The values on the control lines tell the multiplexer which one of the data inputs should be passed through the circuit to the output. The purpose of the MUX is to select information from one of several input terminals and route that input to a single output lead. 
		 An application of a multiplexer is a parallel-to-serial converter. By putting 4 bits of data on the input lines of a 4-to-1 MUX, and then stepping the control lines sequentially from 00 to 11 (binary: 00 01 10 11), the 4 bits are put onto the output line in series. 


THE GRAPHIC WINDOW

		 The Graphic Window shows a four-input multiplexer circuit (4-to-1 MUX). As defined above, a multiplexer has 2^n data inputs, one output, and n control lines. In this example, n = 2 (two control lines: S1, S0).  Then:

     # of control lines = n = 2
     # of data inputs  = 2^n = 2^2 = 4
     # of outputs = 1 

		 The data on the four data input lines, D0-D3, are arbitrary values. Assume these "1's" and "0's" are data coming from some other part of a larger circuit.
		 The Edit Boxes allow you to alter the values of the control lines. Enter a "1" or a "0" in each of the Edit Boxes, then run the simulation to see the MUX output.
 
control lines    output
  S1   S0          F

   0    0         DO
   0    1         D1
   1    0         D2
   1    1         D3


Lesson 8 
8-Bit Arithmetic Logic Unit

		 The 1-bit ALU of Lesson 7 is available as a chip known as a "bit slice". Bit slices allow an ALU to be built to any desired "width" (word size, number of bits). By joining eight 1-bit slices together, an 8-bit ALU is formed. To join two 1-bit ALUs, the Carry Out (CO) of one is used as the Carry In (CI) of the next. An 8-bit ALU is more practical than the 1-bit ALU because it can perform logic and addition operations on two 8-bit words (on two bytes). 
		 The 8-bit ALU has two features not found on the 1-bit ALU: a zero flag (Z flag) and a negative flag (N flag). Flags are control outputs. They indicate whether or not a particular condition has been met. This information may be passed along the output to control some other component in a larger circuit.
		 If the result of the ALU operation is zero, the Z flag is "set" (given a value of "1", "on"). If the result of the ALU operation is a negative value, the N flag is "set" (set to "1", "on"). Two other control outputs that an ALU may have are flags to indicate that an overflow or a carry out of the highest bit has occurred. 


{ *************************** }
The following section describes the ALU's representation of negative numbers. If you set S1=1,S0=1 for addition, and you do not understand how the sum of the A and B words was determined, read this section.  
{ *************************** }

¥ THE ALUs EXPRESSION OF NUMBERS IN 2's COMPLEMENT FORM 

		 The 8-bit ALU of this example uses "2's complement form" to express negative numbers. To express a binary negative number in 2's complement, follow these steps:

	1)  start with the binary value of the absolute value of the base10 number
	2)  reverse each digit of the positive number 
		    (all "1's" become "0's", all "0's" become "1's")
	3)  add "1" to the result


EXAMPLE 1, 2's complement:
		 Express decimal "-5" in 2's complement form using the above steps.

	1)   absolute value of -5  =  5
	
		   5 base10  =  00000101 base2

	2)   reverse digits:   00000101  -->>  11111010

	3)   add "1"    11111010
                +       1
                __________
                 11111011

 	result:   -5 base10  =  11111011 base2, 2's complement


		 Note that the High Order bit (HO,leftmost bit) of the result (11111011) is a "1". This will ALWAYS be the case. This fact is made use of by the ALU's N flag. The ALU simply "taps" the High Order bit of the result (F7) and sends it out as the N flag. An N flag value of "1" means the ALU result is negative.


EXAMPLE 2, addition involving a negative number:
		 Express the sum of decimal "-5" added to decimal "+3" as a binary number.

  ¥   from Example 1, -5 base10  =  11111011 base2, 2's complement

  ¥   +3 base10  =  00000011 base2

  ¥   -5 + (+3):      11111011    (-5)
                    + 00000011    (+3)
                    __________
                      11111110    (-2)

  result:   -5 + (+3) =  11111110  , 2's complement

Note that the High Order bit is a "1", signifying a negative number.

To find the absolute value of a 2's complement number in decimal form, follow these steps:

	1)  reverse each digit of the 2's complement number 
	2)  add "1" to the result

To verify the result of Example 2, use the above steps:

  from result of Example 2: -2 base10   =  11111110   , 2's complement

  1)   reverse digits:   11111110  -->>  00000001

  2)   add "1"    00000001
                +        1
                __________
                  00000010


 	result:   absolute value of -5 + (+3) = 2


THE GRAPHIC WINDOW

		 The Graphic Window shows an 8-bit arithmetic logic unit (8-bit ALU). The Edit Boxes allow you to enter two 8-bit words (A and B) that will have some logic operation or addition performed on them. You may also enter the control (select) line values (S1,S0) that determine which operation will be performed. The ALU is capable of performing one of four operations (the same four operations as the 1-bit ALU, Lesson 7): 

   ¥ logic AND on A,B
   ¥ logic OR on A,B
   ¥ invert B
   ¥ add A to B

Enter a "1" or a "0" in each of the Edit Boxes, then run the simulation. The result of the logic operation appears at output F.

  control lines    output result
     S2   S1        (operation)

     0    0          A AND B
     0    1          A OR B
     1    0          B NOT (invert B)
     1    1          A + B (A added to B)
 


